/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.aceros;

import ar.com.wholeconsulting.pacc.gui.utils.frame.SaveOnCloseDialog;
import ar.com.wholeconsulting.pacc.model.Acero;
import ar.com.wholeconsulting.pacc.persistance.JPASession;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Duco Pablo
 */
public class FrameAceros extends SaveOnCloseDialog {

    private List<Acero> newObjects = new LinkedList<Acero>();

    /**
     * Creates new form FrameFluidos
     */
    public FrameAceros(JFrame frame) {
        super(frame, true);
        initComponents();
        initComponentsView();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableAceros = new javax.swing.JTable();
        buttonAdd = new javax.swing.JButton();
        buttonRemove = new javax.swing.JButton();
        buttonCerrar = new javax.swing.JButton();
        buttonGuardar = new javax.swing.JButton();

        setTitle("Editor de Aceros");
        setResizable(false);

        tableAceros.setModel(new AceroModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "150", "200", "250", "300", "400", "500", "600", "650", "700", "750", "800", "850", "900"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tableAceros.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableAceros.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableAceros);

        buttonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/add_16x16.gif"))); // NOI18N
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });

        buttonRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/delete_16x16.gif"))); // NOI18N
        buttonRemove.setEnabled(false);
        buttonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoveActionPerformed(evt);
            }
        });

        buttonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/trash_16x16.gif"))); // NOI18N
        buttonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCerrarActionPerformed(evt);
            }
        });

        buttonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/save_16x16.gif"))); // NOI18N
        buttonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 924, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 872, Short.MAX_VALUE)
                        .addComponent(buttonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCerrar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(buttonAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonRemove)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAdd)
                    .addComponent(buttonRemove))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCerrar)
                    .addComponent(buttonGuardar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-960)/2, (screenSize.height-503)/2, 960, 503);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
        Acero acero = new Acero();
        ((AceroModel) tableAceros.getModel()).addRow(acero);
        newObjects.add(acero);
        tableAceros.updateUI();
    }//GEN-LAST:event_buttonAddActionPerformed

    private void buttonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
        int row = tableAceros.getSelectedRow();
        Acero obj = (Acero) tableAceros.getModel().getValueAt(row, 0);
        if (newObjects.contains(obj)) {
            newObjects.remove(obj);
        } else {
            JPASession.getInstance().getManager().remove(obj);
        }
        ((AceroModel) tableAceros.getModel()).removeRow(row);
        tableAceros.updateUI();
    }//GEN-LAST:event_buttonRemoveActionPerformed
    private void buttonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCerrarActionPerformed
        ok = false;
        this.setVisible(false);
    }//GEN-LAST:event_buttonCerrarActionPerformed

    private void buttonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGuardarActionPerformed
        ok = true;
        this.setVisible(false);
    }//GEN-LAST:event_buttonGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonCerrar;
    private javax.swing.JButton buttonGuardar;
    private javax.swing.JButton buttonRemove;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableAceros;
    // End of variables declaration//GEN-END:variables

    private void initComponentsView() {
        List<Object> list = JPASession.getInstance().getResultListNamedQuery(Acero.FIND_ALL);
        for (Object obj : list) {
            ((AceroModel) tableAceros.getModel()).addRow((Acero) obj);
        }
        tableAceros.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                boolean enable = false;
                if (tableAceros.getSelectedRow() != -1) {
                    enable = true;
                }
                buttonRemove.setEnabled(enable);
            }
        });
    }

    @Override
    protected void saveNewObjects() {
        for (Acero acero : newObjects) {
            try {
                JPASession.getInstance().getManager().persist(acero);
            } catch (Exception ex) {
                Logger.getLogger(FrameAceros.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
